var xmlHttp
var id
var post
var text
var state
var city
var bmarker = []
var overlays = []
var map
var baseIcon


function get_state(state)
{ 

//alert(post)

//zip = document.getElementById("cities").value

var url="get_state.php?state="+state
//alert(url)
xmlHttp=GetXmlHttpObject(stateChanged3)
xmlHttp.open("Get", url , true)
xmlHttp.send(null)


} 
function get_city(state)
{ 

//alert(post)
document.getElementById('city_select').innerHTML="<img width=15 height=15 src=../images/ani.gif>"
//zip = document.getElementById("cities").value

var url="get_city.php?state="+state
//alert(url)
xmlHttp=GetXmlHttpObject(stateChanged3)
xmlHttp.open("Get", url , true)
xmlHttp.send(null)

}

function get_codes()
{ 

document.getElementById('map').innerHTML="<img src=../images/ani.gif>"

var select_form_city = document.getElementById('select_city_form');
var city = select_form_city.options[select_form_city.selectedIndex].value;

var box = document.getElementById('state');
var state = box.options[box.selectedIndex].value;

var url="get_state.php?state="+state+"&city="+city
//alert(url)
xmlHttp=GetXmlHttpObject(stateChanged)
xmlHttp.open("Get", url , true)
xmlHttp.send(null)

}

function map_moved(map)
{

	
	var overlay = new GOverlay();
	for(var w = 0 ; w < overlays.length; w++)
	{
		//alert('asd')
		map.removeOverlay(overlays[w]);
		//alert('sadssaddsad')
	}
	
	
	var sw = map.getBounds().getSouthWest();
	var ne = map.getBounds().getNorthEast();
	var top, bottom, left, right;
	top = ne.lat();
	bottom = sw.lat();
	right = ne.lng();
	left = sw.lng();
				
	
GDownloadUrl("get_points_in_bounds.php?top="+top+"&bottom="+bottom+"&left="+left+"&right="+right, function(data, responseCode) 
{
//alert(data)
  var xml = GXml.parse(data);

  var markers = xml.documentElement.getElementsByTagName("marker");
  for (var i = 0; i < markers.length; i++) 
  {
    var point = new GLatLng(markers[i].getAttribute("lat"), markers[i].getAttribute("lng"));
	var url = markers[i].getAttribute("id")
	//alert (url)
	//alert('1')
	overlay = createMarkers(point, i, url);
	//alert('2')
	overlays[i] = overlay;
	//alert('3')
    map.addOverlay(overlay);
	
  }
});

}

function createMarkers(point, number, url) 
{	
		
		
		  var gmarker = new GMarker(point, baseIcon);
		  
		  GEvent.addListener(gmarker, "click", function() 
		  {
			var string = '';
			GDownloadUrl("get_links_from_blog.php?url="+url, function(data, responseCode)
		  	{
			  
			  	var xml2 = GXml.parse(data);
			    var markers2 = xml2.documentElement.getElementsByTagName("marker");
				
				  for (var i = 0; i < markers2.length; i++) 
				  {
					
					string = string+"<a href="+GXml.value(markers2[i].getElementsByTagName('link')[0])+">"+GXml.value(markers2[i].getElementsByTagName('title')[0])+"</a><BR>";
					//
					
				  }
				  //alert(string)
			gmarker.openInfoWindowHtml(string);
			});
			//alert(string)
			
			
		  
		  });
		  return gmarker;
}



function stateChanged() 
{ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	text = xmlHttp.responseText
	//alert(text)
	load(text)
	} 
} 


function stateChanged3() 
{ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	text = xmlHttp.responseText
	//alert(text)
	show_cities(text)
	} 
} 

function show_cities(text)
{
	document.getElementById('city_select').disabled = false
	document.getElementById('city_select').innerHTML=text
}

function load(result) {
      if (GBrowserIsCompatible()) {
	  //alert(result)
	  //get_state(state)
	  var long_lat = result.split('|')
	  
	  baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.image = "blue_MarkerB.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
	  
 		map = new GMap2(document.getElementById("map"));
        
			var center = new GLatLng(long_lat[1], long_lat[0]);
			map.addControl(new GLargeMapControl());
			map.addControl(new GOverviewMapControl());
			map.setCenter(center, 10);
			
			var marker = new GMarker(center, {draggable: true});
			
			GEvent.addListener(map, "dragend", function()
			 	{
				map_moved(map);

				}
			 );
			
			GEvent.addListener(map, "dblclick", function()
			 	{
				 //load the xml points

				}
			 );
			
			
			GEvent.addListener(marker, "dragstart", function() {
			  map.closeInfoWindow();
			  });
			
			GEvent.addListener(marker, "dragend", function() {
			var lat = marker.getPoint().lat();
			var lng = marker.getPoint().lng();	
			 document.getElementById('show_num').innerHTML = '<input id="hlat" name="hlat" type="hidden" value="'+lat+'" /><input name="hlng" id="hlng" type="hidden" value="'+lng+'" /><b>Location:</b><BR>Latitude: '+lat+"<BR>Longitude:"+lng
			  });
			
			map.addOverlay(marker);
			map_moved(map);

			
      }
    }
	

function GetXmlHttpObject(handler)
{ 
var objXmlHttp=null

if (navigator.userAgent.indexOf("Opera")>=0)
{
alert("This example doesn't work in Opera") 
return 
}
if (navigator.userAgent.indexOf("MSIE")>=0)
{ 
var strName="Msxml2.XMLHTTP"
if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName="Microsoft.XMLHTTP"
} 
try
{ 
objXmlHttp=new ActiveXObject(strName)
objXmlHttp.onreadystatechange=handler 
return objXmlHttp
} 
catch(e)
{ 
alert("Error. Scripting for ActiveX might be disabled") 
return 
} 
} 
if (navigator.userAgent.indexOf("Mozilla")>=0)
{
objXmlHttp=new XMLHttpRequest()
objXmlHttp.onload=handler
objXmlHttp.onerror=handler 
return objXmlHttp
}
} 